/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgAddPatient.java
 *
 * Created on Aug 18, 2012, 2:23:16 PM
 */

package Model.dialog;

import DataBase.DBManagerment;
import Processing.PikaDate;
import Processing.dbclass.Patient;
import java.sql.Connection;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class dlgEditPatient extends javax.swing.JDialog {

    /** Creates new form dlgAddPatient */
    public dlgEditPatient(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        PikaDate piDAte = new PikaDate();
        cbbPatientYear.setModel(piDAte.getListYear());
    }

        /**
     * Đối tượng của class DBManagerment
     */
    private DBManagerment db = new DBManagerment();

    /**
     * Một Connection
     */
    private Connection con = db.ConnectServer();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        pnlPatientAdd = new javax.swing.JPanel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        txtPatientLastName = new javax.swing.JTextField();
        txtPatientFirstName = new javax.swing.JTextField();
        txtPatientAddID = new javax.swing.JTextField();
        jLabel22 = new javax.swing.JLabel();
        txtPatientPhoneNo = new javax.swing.JTextField();
        rdbPatientMale = new javax.swing.JRadioButton();
        jLabel23 = new javax.swing.JLabel();
        rdbPatientFemale = new javax.swing.JRadioButton();
        jScrollPane10 = new javax.swing.JScrollPane();
        taPatientAdress = new javax.swing.JTextArea();
        btnPatientAction = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel26 = new javax.swing.JLabel();
        cbbPatientYear = new javax.swing.JComboBox();
        cbbPatientMonth = new javax.swing.JComboBox();
        cbbPatientDay = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edit Patient");
        setResizable(false);

        pnlPatientAdd.setMaximumSize(new java.awt.Dimension(295, 360));
        pnlPatientAdd.setPreferredSize(new java.awt.Dimension(295, 360));

        jLabel14.setText("Patient's ID:");

        jLabel15.setText("First Name:");

        jLabel16.setText("Last Name:");

        jLabel21.setText("Address:");

        txtPatientAddID.setEnabled(false);

        jLabel22.setText("Phone No:");

        buttonGroup1.add(rdbPatientMale);
        rdbPatientMale.setSelected(true);
        rdbPatientMale.setText("Male");

        jLabel23.setText("Gender:");

        buttonGroup1.add(rdbPatientFemale);
        rdbPatientFemale.setText("Female");

        taPatientAdress.setColumns(20);
        taPatientAdress.setRows(5);
        jScrollPane10.setViewportView(taPatientAdress);

        btnPatientAction.setText("Edit");
        btnPatientAction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPatientActionActionPerformed(evt);
            }
        });

        jButton4.setText("Cancel");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel26.setText("Date of Birth:");

        cbbPatientYear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1990", "1989" }));
        cbbPatientYear.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbbPatientYearItemStateChanged(evt);
            }
        });

        cbbPatientMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));
        cbbPatientMonth.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbbPatientMonthItemStateChanged(evt);
            }
        });

        cbbPatientDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        javax.swing.GroupLayout pnlPatientAddLayout = new javax.swing.GroupLayout(pnlPatientAdd);
        pnlPatientAdd.setLayout(pnlPatientAddLayout);
        pnlPatientAddLayout.setHorizontalGroup(
            pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPatientAddLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlPatientAddLayout.createSequentialGroup()
                        .addGroup(pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel22)
                            .addComponent(jLabel23)
                            .addComponent(jLabel26))
                        .addGap(21, 21, 21)
                        .addGroup(pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlPatientAddLayout.createSequentialGroup()
                                .addComponent(cbbPatientYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbbPatientMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbbPatientDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnlPatientAddLayout.createSequentialGroup()
                                .addComponent(rdbPatientMale)
                                .addGap(18, 18, 18)
                                .addComponent(rdbPatientFemale))
                            .addComponent(txtPatientPhoneNo, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)))
                    .addGroup(pnlPatientAddLayout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(btnPatientAction, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlPatientAddLayout.createSequentialGroup()
                        .addGroup(pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel14)
                            .addComponent(jLabel16)
                            .addComponent(jLabel15))
                        .addGap(27, 27, 27)
                        .addGroup(pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPatientFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                            .addComponent(txtPatientAddID, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPatientLastName, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)))
                    .addGroup(pnlPatientAddLayout.createSequentialGroup()
                        .addComponent(jLabel21)
                        .addGap(43, 43, 43)
                        .addComponent(jScrollPane10, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(38, 38, 38))
        );
        pnlPatientAddLayout.setVerticalGroup(
            pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPatientAddLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(txtPatientAddID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPatientFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPatientLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel21)
                    .addComponent(jScrollPane10, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlPatientAddLayout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(jLabel22))
                    .addGroup(pnlPatientAddLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtPatientPhoneNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(10, 10, 10)
                .addGroup(pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rdbPatientMale)
                    .addComponent(jLabel23)
                    .addComponent(rdbPatientFemale))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel26)
                    .addComponent(cbbPatientYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbPatientMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbPatientDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlPatientAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPatientAction)
                    .addComponent(jButton4))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        getContentPane().add(pnlPatientAdd, java.awt.BorderLayout.WEST);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbbPatientYearItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbbPatientYearItemStateChanged
        // TODO add your handling code here:
        PikaDate pika = new PikaDate();
        int month = Integer.parseInt(cbbPatientMonth.getSelectedItem().toString());
        int year = Integer.parseInt(cbbPatientYear.getSelectedItem().toString());
        cbbPatientDay.setModel(pika.getDayByMonth(month, year));
}//GEN-LAST:event_cbbPatientYearItemStateChanged

    private void cbbPatientMonthItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbbPatientMonthItemStateChanged
        // TODO add your handling code here:
        PikaDate pika = new PikaDate();
        int month = Integer.parseInt(cbbPatientMonth.getSelectedItem().toString());
        int year = Integer.parseInt(cbbPatientYear.getSelectedItem().toString());
        cbbPatientDay.setModel(pika.getDayByMonth(month, year));
}//GEN-LAST:event_cbbPatientMonthItemStateChanged

    private void btnPatientActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPatientActionActionPerformed
        // TODO add your handling code here:
        if (txtPatientPhoneNo.getText().length() != 0) {
            try {
                int it_av = Integer.parseInt(txtPatientPhoneNo.getText());

                if (it_av == 0) {
                    return;

                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(this, "Phone must digit only!");
                return;
            }
        }


        if (txtPatientFirstName.getText().length() == 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Patient's first name must have!");
        } else if (txtPatientLastName.getText().length() == 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Patient's last name must have!");
        }else{
            // Trong add bill này CollectorID có thể để trống nên ko cần kiểm tra

            // Nếu toàn bộ quá trình kiểm tra ko sai thì bắt đầu add vào

            // Tạo mới một Object Bill để chứa thông tin
            pat = new Patient();

            pat.setFirstName(txtPatientFirstName.getText());

            pat.setLastName(txtPatientLastName.getText());

            pat.setAddress(taPatientAdress.getText());

            pat.setPhoneNo(txtPatientPhoneNo.getText());

            if (rdbPatientMale.isSelected()) {
                pat.setGender(true);
            }else{
                pat.setGender(false);
            }

            PikaDate pik = new PikaDate();

            pik.setYear(Integer.parseInt(cbbPatientYear.getSelectedItem().toString()));

            pik.setMonth(Integer.parseInt(cbbPatientMonth.getSelectedItem().toString()));

            pik.setDate(Integer.parseInt(cbbPatientDay.getSelectedItem().toString()));

            pat.setDateOfBirth(pik);

            this.setVisible(false);

            // DateMade và DatePaid ko phải làm vì tớ đã sửa lại method add trong
            // DBManager, sẽ lấy ngày hiện tại
            // Report có DateMade tương tự cũng được tớ sủa method rồi
        }
    }//GEN-LAST:event_btnPatientActionActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgEditPatient dialog = new dlgEditPatient(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    /**
     * Biến chứa dữ liệu của Test
     */
    private Patient pat;

    /**
     * Method trả về bill để chương trình xử lý
     * @return Test
     */
    public Patient getPatient() {
        return pat;
    }

    public void setPatient(Patient pat) {
        this.pat = pat;

        txtPatientAddID.setText(""+pat.getPatientID());

        txtPatientFirstName.setText(db.generatePatient(con, pat.getPatientID()).getFirstName());
        txtPatientLastName.setText(db.generatePatient(con, pat.getPatientID()).getLastName());
        txtPatientPhoneNo.setText(db.generatePatient(con, pat.getPatientID()).getPhoneNo());
        taPatientAdress.setText(db.generatePatient(con, pat.getPatientID()).getAddress());

        if (db.generatePatient(con, pat.getPatientID()).isGender()) {
            rdbPatientMale.setSelected(true);
        } else {
            rdbPatientFemale.setSelected(true);
        }

        PikaDate pik = db.generatePatient(con, pat.getPatientID()).getDateOfBirth();

        cbbPatientYear.setSelectedItem(pik.getYear());
        cbbPatientMonth.setSelectedItem(""+pik.getMonth());
        cbbPatientDay.setSelectedItem(pik.getDate());

        pat.setDateOfBirth(pik);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPatientAction;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbbPatientDay;
    private javax.swing.JComboBox cbbPatientMonth;
    private javax.swing.JComboBox cbbPatientYear;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JScrollPane jScrollPane10;
    private javax.swing.JPanel pnlPatientAdd;
    private javax.swing.JRadioButton rdbPatientFemale;
    private javax.swing.JRadioButton rdbPatientMale;
    private javax.swing.JTextArea taPatientAdress;
    private javax.swing.JTextField txtPatientAddID;
    private javax.swing.JTextField txtPatientFirstName;
    private javax.swing.JTextField txtPatientLastName;
    private javax.swing.JTextField txtPatientPhoneNo;
    // End of variables declaration//GEN-END:variables

}
